package com.wang.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo2 {
    public static void main(String[] args) {
        Collection<Student> collection = new ArrayList<>();
        Student s1 = new Student("wang",12),
                s2 = new Student("zi",13),
                s3 = new Student("qi",14);
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection.size());
        System.out.println(collection.toString());
        //下面这样再new一个判断是判断不到的,每new一个对象,就相当于再堆上创建一个新对象

        if (collection.contains(new Student("qi",14))){//只能用s1,s2,s3这些变量进行判断
            System.out.println("存在qi");
        }
        for (Student student: collection) {
            System.out.println(student);
        }
        //迭代器版本
        Iterator<Student> iterator = collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
